﻿using System;

namespace UniRx.Operators
{
	// Token: 0x0200027F RID: 639
	internal class CreateSafeObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000E5D RID: 3677 RVA: 0x000439F4 File Offset: 0x00041DF4
		public CreateSafeObservable(Func<IObserver<T>, IDisposable> subscribe) : base(true)
		{
			this.subscribe = subscribe;
		}

		// Token: 0x06000E5E RID: 3678 RVA: 0x00043A04 File Offset: 0x00041E04
		public CreateSafeObservable(Func<IObserver<T>, IDisposable> subscribe, bool isRequiredSubscribeOnCurrentThread) : base(isRequiredSubscribeOnCurrentThread)
		{
			this.subscribe = subscribe;
		}

		// Token: 0x06000E5F RID: 3679 RVA: 0x00043A14 File Offset: 0x00041E14
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			observer = new CreateSafeObservable<T>.CreateSafe(observer, cancel);
			return this.subscribe(observer) ?? Disposable.Empty;
		}

		// Token: 0x0400084C RID: 2124
		private readonly Func<IObserver<T>, IDisposable> subscribe;

		// Token: 0x02000280 RID: 640
		private class CreateSafe : OperatorObserverBase<T, T>
		{
			// Token: 0x06000E60 RID: 3680 RVA: 0x00043A37 File Offset: 0x00041E37
			public CreateSafe(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
			}

			// Token: 0x06000E61 RID: 3681 RVA: 0x00043A44 File Offset: 0x00041E44
			public override void OnNext(T value)
			{
				try
				{
					this.observer.OnNext(value);
				}
				catch
				{
					base.Dispose();
					throw;
				}
			}

			// Token: 0x06000E62 RID: 3682 RVA: 0x00043A80 File Offset: 0x00041E80
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000E63 RID: 3683 RVA: 0x00043AB8 File Offset: 0x00041EB8
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}
		}
	}
}
